package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.CodeRuleSequenceNumService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.coderulesequencenum.vo.*;
import com.ibigcat.module.mdm.dao.entity.CodeRuleSequenceNumDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.CodeRuleSequenceNumMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 编码规则序号 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CodeRuleSequenceNumServiceImpl implements CodeRuleSequenceNumService {

    @Resource
    private CodeRuleSequenceNumMapper codeRuleSequenceNumMapper;

    @Override
    public Long createCodeRuleSequenceNum(CodeRuleSequenceNumSaveReqVO createReqVO) {
        // 插入
        CodeRuleSequenceNumDO codeRuleSequenceNum = BeanUtils.toBean(createReqVO, CodeRuleSequenceNumDO.class);
        codeRuleSequenceNumMapper.insert(codeRuleSequenceNum);
        // 返回
        return codeRuleSequenceNum.getId();
    }

    @Override
    public void updateCodeRuleSequenceNum(CodeRuleSequenceNumSaveReqVO updateReqVO) {
        // 校验存在
        validateCodeRuleSequenceNumExists(updateReqVO.getId());
        // 更新
        CodeRuleSequenceNumDO updateObj = BeanUtils.toBean(updateReqVO, CodeRuleSequenceNumDO.class);
        codeRuleSequenceNumMapper.updateById(updateObj);
    }

    @Override
    public void deleteCodeRuleSequenceNum(Long id) {
        // 校验存在
        validateCodeRuleSequenceNumExists(id);
        // 删除
        codeRuleSequenceNumMapper.deleteById(id);
    }

    private void validateCodeRuleSequenceNumExists(Long id) {
        if (codeRuleSequenceNumMapper.selectById(id) == null) {
            throw exception(CODE_RULE_SEQUENCE_NUM_NOT_EXISTS);
        }
    }

    @Override
    public CodeRuleSequenceNumDO getCodeRuleSequenceNum(Long id) {
        return codeRuleSequenceNumMapper.selectById(id);
    }

    @Override
    public PageResult<CodeRuleSequenceNumDO> getCodeRuleSequenceNumPage(CodeRuleSequenceNumPageReqVO pageReqVO) {
        return codeRuleSequenceNumMapper.selectPage(pageReqVO);
    }

}